package class

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/pkg/auth"

	"github.com/lab-online/internal/class/domain"
	"github.com/lab-online/internal/class/export"
	"github.com/lab-online/internal/class/handler"
	"github.com/lab-online/internal/class/infra/repository"
	"github.com/lab-online/internal/class/interface/rest"
)

var (
	NewRepository = repository.New
	NewDomain     = domain.New
	NewExport     = export.New
)

type Route struct {
	rest.ClassHandler
	auth auth.Middleware
}

func NewRoute(auth auth.Middleware, domain domain.ClassDomain) *Route {
	h := handler.New(domain)
	return &Route{h, auth}
}

func (r *Route) Register(router *gin.RouterGroup) {
	class := router.Group("/classes")

	class.GET("", r.auth(auth.RoleAll), handler.SearchClassValidator, r.SearchClass)
	class.GET("/:id", r.auth(auth.RoleAll), handler.GetClassValidator, r.GetClass)

	class.Use(r.auth(auth.RoleTeacher | auth.RoleAdmin))
	class.POST("", handler.CreateClassValidator, r.CreateClass)
	class.DELETE("/:id", handler.RemoveClassValidator, r.RemoveClass)
	class.PATCH("/:id", handler.UpdateClassValidator, r.UpdateClass)

	class.GET("/:id/students", handler.GetClassValidator, r.GetClassStudents)
	class.PATCH("/:id/students", handler.UpdateStudentValidator, r.UpdateStudent)
}
